// pages/list/list.js
const app = getApp()
Page({
     data:{
      activity:true,
      full:null,
      decrease:null,
       listData:[],
       cartList:[], //购物车
       activeIndex: 0,  //左侧菜单当前选中索引号
       loading:true, //显示加载效果
       currentType :0, //当前选择添加到购物车的食品类型索引
       currentIndex :0,   //当前选择添加到购物车的食品的列表索引
       sumMoney:0,  //购物车总价钱
       cupNumber:0,  //购物车商品总数量 （小数字图标）
       showCart:false, //是否展开购物车
     },
    //点击左侧菜单选项
     selectMenu:function(e){
       var index= e.currentTarget.dataset.index;
       this.setData(
         {
           activeIndex: index,
           toView: 'a'+index
         }
       )
     },

     //加入购物车
     addToCart:function(e){
       var type = e.currentTarget.dataset.type; //食品类别
       var index = e.currentTarget.dataset.index ;//食品的索引
       this.setData({
         currentType: type,
         currentIndex: index
       })
       var a = this.data;
       var cartList = a.cartList;
       //获取加入购物车食品的id，将相同食品合并
       var id= a.listData[a.currentType].foods[a.currentIndex].dishId;
       var isOrdered = false;
       for(var i in cartList){
         if(id == cartList[i].id){
           this.addNum(i,cartList); //已点过此食品，则点购数量加一
           isOrdered = true;
           break;
         }
       }
       if(!isOrdered){ //未点过此食品，将食品信息添加入购物车
          //获取新添加食品信息
          var addItem = {
            'id' : a.listData[a.currentType].foods[a.currentIndex].dishId,
            'name' : a.listData[a.currentType].foods[a.currentIndex].dishName,
            'price' : a.listData[a.currentType].foods[a.currentIndex].price,
            'image': a.listData[a.currentType].foods[a.currentIndex].image,
            'number':1,
            'sum':a.listData[a.currentType].foods[a.currentIndex].price, 
          }  
        //计算总价
         var sumMoney = a.sumMoney+a.listData[a.currentType].foods[a.currentIndex].price;       

         //将新食品添加到购物车数组
         cartList.push(addItem);

         this.setData({
           cartList:cartList,
           sumMoney:sumMoney,
           cupNumber:a.cupNumber+1
         })
        }
     },

     //往购物车中增加已有商品数量
     addNum:function(index,cartList){
         cartList[index].number++;
         var sum= this.data.sumMoney+ cartList[index].price; //计算购物车总价
         
         cartList[index].sum += cartList[index].price;  //计算当前商品的总价

         this.setData({
           cartList: cartList,
           sumMoney:sum,
           cupNumber:this.data.cupNumber+1
         })
     },
 
    //在购物车列表中点击“+”，增加商品数量 
    addNumber :function(e){
      var index = e.currentTarget.dataset.index; //获取子商品在购物车列表中的索引
      var cartList = this.data.cartList;
      this.addNum(index,cartList);

    },

    decNumber:function(e){
      var index = e.currentTarget.dataset.index; //获取子商品在购物车列表中的索引
      var cartList = this.data.cartList;
      var sum= this.data.sumMoney - cartList[index].price; //计算购物车总价
      cartList[index].sum -= cartList[index].price;  //计算当前商品的总价

      //如果当前商品数量为1，则点击-号后将其商品去除，否则只需要数量减1
      cartList[index].number==1?cartList.splice(index,1):cartList[index].number--;
      this.setData({
        cartList:cartList,
        sumMoney:sum,
        cupNumber:this.data.cupNumber-1,
        showCart:cartList.length==0? false:true
      })
    },
 
    //清空购物车
    clearCartList:function(){
        this.setData({
          cartList:[],
          showCart:false,
          sumMoney:0,
          cupNumber:0
        })
    },

     //展开购物车
     showCartList:function(){
       if(this.data.cartList.length != 0){
         this.setData({
           showCart: !this.data.showCart
         })
       }
     },

     //点击选好了
     goBalance:function(e){
      if(this.data.sumMoney==0){
        return;
      }
      app.globalData.carList = this.data.cartList;
     wx.navigateTo({
       url: '../order/balance/balance',
     })
     },
     onShow:function(){
      var that = this;
      wx.request({
        url: 'http://localhost:8080/dish/allType', 
        method:"GET",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success:function (res) {
          that.setData({
            listData:res.data.list
          })
        }
      }),
      wx.request({
        url: 'http://localhost:8080/activity/all',
        method:"get",
        success:function(res){
          if(res.data.list!=null){
            var i = res.data.list.length-1;
            that.setData({
              full: res.data.list[i].full,
              decrease:res.data.list[i].decrease,
              activity:true
            })
          }else{
            that.setData({
              activity:false
            })
            
          }
          
        }
      })
     }

})